package com.iocollege.tinyframe.service.system.impl;

import com.iocollege.tinyframe.common.core.constant.Constants;
import com.iocollege.tinyframe.common.core.constant.UserConstants;
import com.iocollege.tinyframe.common.core.redis.RedisCache;
import com.iocollege.tinyframe.common.service.impl.BaseMyService;
import com.iocollege.tinyframe.common.utils.StringUtils;
import com.iocollege.tinyframe.entity.system.SysConfig;
import com.iocollege.tinyframe.mapper.system.SysConfigMapper;
import com.iocollege.tinyframe.service.system.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 关注公众号：MarkerHub
 * @since 2021-01-26
 */
@Service
public class SysConfigServiceImpl extends BaseMyService<SysConfigMapper, SysConfig> implements SysConfigService {



    @Autowired
    private RedisCache redisCache;



    /**
     * 校验参数键名是否唯一
     *
     * @param config 参数配置信息
     * @return 结果
     */
    @Override
    public String checkConfigKeyUnique(SysConfig config)
    {
        Long configId = StringUtils.isNull(config.getConfigId()) ? -1L : config.getConfigId();
        SysConfigMapper myMaper = (SysConfigMapper)this.baseMapper;
        SysConfig info = myMaper.checkConfigKeyUnique(config.getConfigKey());
        if (StringUtils.isNotNull(info) && info.getConfigId().longValue() != configId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }


    /**
     * 清空缓存数据
     */
    @Override
    public void clearCache()
    {
        Collection<String> keys = redisCache.keys(Constants.SYS_CONFIG_KEY + "*");
        redisCache.deleteObject(keys);
    }


}
